package com.cunshe.web.manage;

import com.cunshe.common.ResponseVO;
import com.cunshe.config.annotation.Login;
import com.cunshe.domain.Menu;
import com.cunshe.domain.ProductFeature;
import com.cunshe.service.MenuService;
import com.cunshe.service.ProductFeatureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "产品特点")
@Login
@RestController
@RequestMapping(value = "/manage/productFeature")
public class ProductFeatureController {

    @Autowired
    private ProductFeatureService productFeatureService;
    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "新增")
    @PostMapping(value = "insert")
    public ResponseVO insert(@RequestBody ProductFeature record) {
        productFeatureService.insert(record);
        return ResponseVO.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "delete")
    public ResponseVO delete(@RequestParam("id") Long id) {
        int delete = productFeatureService.delete(id);
        return ResponseVO.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping(value = "update")
    public ResponseVO update(@RequestBody ProductFeature record) {
        int update = productFeatureService.update(record);
        return ResponseVO.success();
    }

    @ApiOperation(value = "查询列表")
    @PostMapping(value = "list")
    public ResponseVO<List<ProductFeature>> list(ProductFeature record) {
        List<ProductFeature> list = productFeatureService.list(record);
        List<Menu> all = menuService.all();
        for (ProductFeature productFeature : list) {
            Menu menu = all.stream()
                    .filter(var -> var.getId().equals(productFeature.getMenuId()))
                    .findFirst()
                    .orElse(Menu.builder().build());
            productFeature.setMenuName(menu.getTitle());
            productFeature.setMenuNameEn(menu.getTitleEn());
        }

        return ResponseVO.success(list);
    }

}